package yzrilyzr.simplesynth.app;
import static yzrilyzr.util.Util.clamp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

import yzrilyzr.ui.uidata;
import yzrilyzr.util.WidgetUtil;

public class WaveDraw extends View{
	static final ArrayList<PointF> ps=new ArrayList<>();
	final Path p=new Path();
	final Paint pt;
	final ArrayList<PointF> ps2=new ArrayList<>();
	final ArrayList<PointF> ps3=new ArrayList<>();
	int ww=0;
	int[] data;
	public WaveDraw(Context c){
		super(c);
		pt=new Paint(Paint.ANTI_ALIAS_FLAG);
		if(ps.size()==0) for(int i=0;i<15;i++) ps.add(new PointF(0,0));
	}
	public void setData(int[] data){
		this.data=data;
	}
	@Override
	public boolean onTouchEvent(MotionEvent event){
		getParent().requestDisallowInterceptTouchEvent(true);
		int a=event.getAction();
		if(a==MotionEvent.ACTION_DOWN)
			ww=clamp(Math.round(event.getX()*(ps.size()-1)/getWidth()),0,ps.size()-1);
		else ps.get(ww).y=(Math.round(
				(event.getY()-getHeight()/2f)*32767/getHeight()/409.6f))*409.6f;
		invalidate();
		return true;
	}
	@Override
	protected void onDraw(Canvas canvas){
		p.reset();
		float cy=getHeight()/2f;
		canvas.drawColor(uidata.BUTTON);
		pt.setColor(uidata.TEXTMAIN);
		pt.setStyle(Paint.Style.STROKE);
		pt.setStrokeWidth(WidgetUtil.px(2));
		p.moveTo(0,cy);
		p.lineTo(getWidth(),cy);
		canvas.drawPath(p,pt);
		p.reset();
		if(ps2.size()!=ps.size()){
			ps2.clear();
			for(PointF p: ps) ps2.add(new PointF(p.x,p.y));
		}
		for(int o=0;o<ps.size();o++){
			PointF l=ps2.get(o);
			l.x=o*getWidth()/(float)(ps.size()-1);
			l.y=ps.get(o).y*getHeight()/32767f+cy;
			canvas.drawCircle(l.x,l.y,WidgetUtil.px(4),pt);
		}
		p.moveTo(0,cy);
		ps3.clear();
		Catmull_Rom(ps2,150,p,ps3);
		if(data!=null) for(int i=0;i<data.length;i++){
			data[i]=(int)(ps3.get(
					clamp(i*ps3.size()/data.length,0,ps3.size()-1)).y-cy)*32767/getHeight();
		}
		pt.setColor(uidata.ACCENT);
		canvas.drawPath(p,pt);
	}
	public static void Catmull_Rom(ArrayList<PointF> point,int cha,Path path,ArrayList<PointF> filled){
		if(cha==0) for(PointF p: point) path.lineTo(p.x,p.y);
		else if(point.size()>1&&cha<10000){
			point.add(0,point.get(0));
			point.add(point.get(point.size()-1));
			for(int index=1;index<point.size()-2;index++){
				PointF p0=point.get(index-1);
				PointF p1=point.get(index);
				PointF p2=point.get(index+1);
				PointF p3=point.get(index+2);
				for(int i=1;i<=cha;i++){
					float t=i*(1.00f/cha);
					float tt=t*t;
					float ttt=tt*t;
					float x=(float)(0.5*(2*p1.x+(p2.x-p0.x)*t+(2*p0.x-5*p1.x+4*p2.x-p3.x)*tt+(3*p1.x-p0.x-3*p2.x+p3.x)*ttt));
					float y=(float)(0.5*(2*p1.y+(p2.y-p0.y)*t+(2*p0.y-5*p1.y+4*p2.y-p3.y)*tt+(3*p1.y-p0.y-3*p2.y+p3.y)*ttt));
					path.lineTo(x,y);
					if(filled!=null) filled.add(new PointF(x,y));
				}
			}
			path.lineTo(point.get(point.size()-1).x,point.get(point.size()-1).y);
			if(filled!=null)
				filled.add(new PointF(point.get(point.size()-1).x,point.get(point.size()-1).y));
		}
	}
	@Override
	protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
		setMeasuredDimension(WidgetUtil.measure(widthMeasureSpec,WidgetUtil.px(-1)),
							 WidgetUtil.measure(heightMeasureSpec,WidgetUtil.px(100)));
	}
}
